package com.xwtec.singleton;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zt
 * @Date: 2021/2/19 22:57
 * 进行双重判空，能够解决mgr05的问题
 */
public class Mgr06 {
    private static volatile Mgr06 INSTANCE;//为什么要加volatile来修饰这个变量，涉及到一个指令重排的问题

    private Mgr06(){};

    public static  Mgr06 getInstance(){
        if(INSTANCE == null){
            synchronized (Mgr06.class){
                if(INSTANCE == null){
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    INSTANCE = new Mgr06();
                }

            }

        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i <100 ; i++) {
            new Thread(()->{
                System.out.println(Mgr06.getInstance().hashCode());
            }).start();
        }
    }
}
